<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/6/9
 * Time: 23:52
 * 视频pv量
 */
require_once dirname(__DIR__)."/common.php";
use app\lib\db\DbException;
use app\cls\tom\SysResource;
use app\cls\tom\Process;

$process = new Process(__FILE__);
$process->isTryError = false;
$process->setProcessNum(1);
$process->initProcess();
$redis = SysResource::getRedis();
$db = SysResource::getDB();
$pv_key = REDIS_PREFIX."tom_lastest_scan_log_id";
do{
    try{
        $query =$db->query("SELECT log_id FROM qmjs_data_logs_2018 ORDER BY log_id DESC LIMIT 1");
        $lastLogInfo = $query->one();
        $lastLogId = !empty($lastLogInfo) ? $lastLogInfo["log_id"] : 0;
        $startId =(int)$redis->get($pv_key);
        $endId = min($startId+10000,$lastLogId);

        //1.用户素材pv查询
        $query = $db->query("SELECT log_uniqueid as uuid,count(*) as num FROM qmjs_data_logs_2018 
where log_position='u_info' and log_operate = 'pv' and log_uniqueid != '' and log_id > {$startId} and log_id <= {$endId} 
GROUP BY log_uniqueid");
        $result = $query->all();
        print_r($result);
        foreach($result as &$row){
            $db->update("user_material",["pv_num"=>"!pv_num+{$row["num"]}"],["user_material_uuid"=>$row["uuid"]]);
        }
        //2.分享查询
        $query = $db->query("SELECT log_uniqueid as uuid,count(*) as num FROM qmjs_data_logs_2018 
where log_operate = 'share' and  log_uniqueid != '' and log_id > {$startId} and log_id <= {$endId} 
GROUP BY log_uniqueid");
        $result = $query->all();
        print_r($result);
        foreach($result as &$row){
            $db->update("user_material",["share_num"=>"!share_num+{$row["num"]}"],["user_material_uuid"=>$row["uuid"]]);
        }
        //3.录音页面pv查询
        $query1 = $db->query("SELECT log_uniqueid as uuid,count(*) as num FROM qmjs_data_logs_2018 
where log_position='record' and log_operate = 'pv' and log_uniqueid != '' and log_id > {$startId} and log_id <= {$endId} 
GROUP BY log_uniqueid");
        $result = $query->all();
        print_r($result);
        foreach($result as &$row){
            $db->update("video",["enter_num"=>"!enter_num+{$row["num"]}"],["material_id"=>$row["uuid"]]);
        }
        $redis->set($pv_key,$endId);
        sleep(2);
    }catch (RedisException $e){
        usleep(500);
    }catch (DbException $e){
        break;
    }
    $process->checkProcessTime();
}while(true);